/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2023-10-20 16:42:26
 * @LastEditors: TT-hzy
 * @LastEditTime: 2024-08-19 11:19:38
 */

import 'package:device_frame/device_frame.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

import '../config/config_index.dart';

import '../utils/utils_index.dart';

/// 6.1
Size screenSize = const Size(390, 844);

/// 4.7
Size screenIphone7 = const Size(375, 667);

/// 5.8
Size screenIphoneX = const Size(375, 812);

/// 初始化 GetX
Widget initGetMaterialApp({
  Widget Function(BuildContext, Widget?)? builder,
}) {
  List<String> launch = LaunchTool.getLocalLaunch();
  ThemeMode themeMode = ThemeTool.getlocalprofileaboutThemeModel();
  return GetMaterialApp(
    /// 入口路由
    initialRoute: RouterS.configNoramlRouts(),

    /// 所有路由集合
    getPages: RouterS.getAllRoutS(),
    defaultTransition: Transition.rightToLeft,
    theme: hzyThemeDataLight,
    darkTheme: hzyThemeDataDark,
    useInheritedMediaQuery: true,
    initialBinding: AllControllerBinding(),

    themeMode: themeMode,
    translations: LaunchConfig(),

    /// 国际化配置 代理
    localizationsDelegates: const [
      GlobalMaterialLocalizations.delegate,
      GlobalWidgetsLocalizations.delegate,
      GlobalCupertinoLocalizations.delegate, // ios
    ],

    /// 配置语言
    locale: Locale(launch[0], launch[1]),
    fallbackLocale: const Locale("zh", "CN"),

    /// 本地支持语言
    supportedLocales: const [
      Locale('zh', 'CN'),
      Locale('en', 'US'),
    ],

    /// 是否显示 导航栏右上角 debug 标识
    debugShowCheckedModeBanner: false,
    builder: builder,
  );
}

/// 初始屏幕适配
Widget initScreenUtil({
  required Widget Function(BuildContext, Widget?) builder,
  bool useInheritedMediaQuery = true,
}) {
  return ScreenUtilInit(
    /// UI尺寸
    designSize: const Size(375, 667),
    useInheritedMediaQuery: useInheritedMediaQuery,
    builder: builder,
  );
}

/// 配置根部手势
initRootGestureDetector({
  Widget? child,
  required BuildContext context,
}) {
  return GestureDetector(
    onTap: () {
      HzyNormalTools.keydissMiss(
        context,
      );
    },
    child: child,
  );
}

initConfig() {
  SystemUiOverlayStyle systemUiOverlayStyle =
      const SystemUiOverlayStyle(statusBarColor: Colors.white);
  SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
}

/// 初始化App
initRunApp() {
  final easyLoading = EasyLoading.init();
  if (!kIsWeb) {
    // initConfig();
  }

  Widget body = initGetMaterialApp(
    builder: (context, child) {
      FlutterView window = View.of(context);

      double width = window.physicalSize.width / window.devicePixelRatio;
      bool hasFrame = width > 500;
      if (hasFrame && kIsWeb) {
        return DeviceFrame(
          device: Devices.ios.iPhone13,
          screen: initScreenUtil(
            builder: (p0, p1) {
              Widget body = initRootGestureDetector(
                child: child,
                context: context,
              );
              FlutterView window1 = View.of(context);
              dPrint(window1.physicalSize.width);
              body = easyLoading(context, body);
              return body;
            },
          ),
        );
      } else {
        return initScreenUtil(
          builder: (p0, p1) {
            Widget body = initRootGestureDetector(
              child: child,
              context: context,
            );
            body = easyLoading(context, body);
            return body;
          },
        );
      }
    },
  );
  return body;
}
